package com.wanmait.myhouse.webController;

import com.wanmait.myhouse.dao.ContactDAO;
import com.wanmait.myhouse.dao.HouseDAO;
import com.wanmait.myhouse.dao.MediatorDAO;
import com.wanmait.myhouse.vo.Contact;
import com.wanmait.myhouse.vo.House;
import com.wanmait.myhouse.vo.Mediator;
import com.wanmait.myhouse.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Controller
@RequestMapping("web")
public class WebContactController {
    @GetMapping("contact")//联系我们
    public String contact(House house, Model model){//传过来的house
        HouseDAO houseDAO = new HouseDAO();
        House house1 = houseDAO.FindById(house.getId());//根据house的Id查出house1，找到中介信息

        MediatorDAO mediatorDAO = new MediatorDAO();
        Mediator mediator = mediatorDAO.findById(house.getMediatorId());
        house1.setMediator(mediator);
        model.addAttribute("house",house1);
        return "/web/contact";
    }

    @PostMapping("updatecontact")
    public String updatecontact (Contact contact){//表单内容写到数据库
        ContactDAO contactDAO = new ContactDAO();
        contact.setDispose(false);
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Timestamp time = Timestamp.valueOf(localDateTime.format(formatter));
        contact.setTime(time);
        contactDAO.addContact(contact);
        return "redirect:/web/index";
    }
}
